/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.exception.RRException;
import io.renren.common.utils.R;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.dao.ImTagDao;
import io.renren.modules.app.entity.ImApplyEntity;
import io.renren.modules.app.entity.ImFriendEntity;
import io.renren.modules.app.entity.ImTagEntity;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.service.ImApplyService;
import io.renren.modules.app.service.ImFriendService;
import io.renren.modules.app.service.UserService;
import io.renren.modules.oss.cloud.OSSFactory;
import io.renren.modules.oss.entity.SysOssEntity;
import io.renren.modules.oss.service.SysOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * APP
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/app/user")
@Api("APP用户接口")
public class AppUserController {
    @Autowired
    private UserService userService;
    @Autowired
    private SysOssService sysOssService;
    @Autowired
    private ImFriendService imFriendService;
    @Autowired
    private ImTagDao imTagDao;

    @Login
    @GetMapping("userInfo")
    @ApiOperation("获取用户信息")
    public R userInfo(@LoginUser UserEntity user){
        return R.ok().put("user", user);
    }

    @Login
    @PostMapping("search")
    @ApiOperation("搜索用户")
    public R search(@RequestBody UserEntity user){
        return R.ok().put("user", userService.findOne(user));
    }

    //详情
    @Login
    @PostMapping("readDetail")
    @ApiOperation("用户详情")
    public R readDetail(@LoginUser UserEntity user,@RequestBody UserEntity us){
        UserEntity u= userService.findOneDetail(us);
        u.setFriend(false);
        if(u==null){
            return R.error("用户不存在");
        }
        ImFriendEntity friend = imFriendService.getOne(new QueryWrapper<ImFriendEntity>().eq("friend_id", us.getUserId()).eq("user_id", user.getUserId()));
        Map<String ,Object> map = new HashMap<>();
        if(friend!=null){
            u.setFriend(true);
            if (!friend.getNickname().equals("")) {
                u.setNickname(friend.getNickname());
            }
            u.setLookme(friend.getLookme());
            u.setLookhim(friend.getLookhim());
            u.setStar(friend.getStar());
            u.setIsblack(friend.getIsblack());
            //获取当前好友标签
            List<ImTagEntity> tagList = imTagDao.selectFriendList(us.getUserId());
            map.put("tagList",tagList);
            //tags: friend.tags.map(item => item.name),
            //朋友圈
            //moments: user.moments
        }
        map.put("user",u);
        return R.ok(map);
    }

    @Login
    @GetMapping("userId")
    @ApiOperation("获取用户ID")
    public R userInfo(@RequestAttribute("userId") Integer userId){
        return R.ok().put("userId", userId);
    }

    @GetMapping("notToken")
    @ApiOperation("忽略Token验证测试")
    public R notToken(){
        return R.ok().put("msg", "无需token也能访问。。。");
    }

    @Login
    @PostMapping("updateUser")
    @ApiOperation("更新用户信息")
    public R updateUser(@RequestBody UserEntity user){
        userService.updateUser(user);
        return R.ok().put("user", user);
    }
    /**
     * 上传文件
     */
    @Login
    @PostMapping("/upload")
    @ApiOperation("上传头像")
    public R upload(@RequestParam("files") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }

        //上传文件
        String suffix = "";
        try{
            suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        }catch (Exception e){
            suffix = ".png";
        }

        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);
        String a = url.substring(32,url.length());
        url = "http://qiniu.doctorfss.com"+a;
        //保存文件信息
        SysOssEntity ossEntity = new SysOssEntity();
        ossEntity.setUrl(url);
        ossEntity.setCreateDate(new Date());
        sysOssService.save(ossEntity);
        return R.ok().put("url", url);
    }
}
